<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data="roles"
                border
                stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row class="border_botton"
                    :class="index === 0 ? 'border_top': ''"
                    v-for="(item,index) in scope.row.children"
                    :key="item.id">
              <el-col :span="8">
                <el-tag>{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="16">
                <el-row :class="index2===0?'':'border_top'"
                        v-for="(item2,index2) in item.children"
                        :key="item2.id">
                  <el-col :span="5">
                    <el-tag type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="19">
                    <el-tag @close="removeTag(scope.row,item3.id)"
                            closable
                            v-for="item3 in item2.children"
                            :key="item3.id"
                            type="warning">
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"
                         label="#">
        </el-table-column>
        <el-table-column prop="roleName"
                         label="角色名称">
        </el-table-column>
        <el-table-column prop="roleDesc"
                         label="角色描述">
        </el-table-column>
        <el-table-column label="操作"
                         width="300">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="primary"
                       icon="el-icon-edit">编辑</el-button>
            <el-button size="mini"
                       type="danger"
                       icon="el-icon-delete">删除</el-button>
            <el-button @click="showSetRights(scope.row)"
                       size="mini"
                       type="warning"
                       icon="el-icon-setting">分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog @close="closeSetDialog"
               title="分配权限"
               :visible.sync="setRightsDialogVisible"
               width="50%">
      <el-tree ref="tree"
               node-key="id"
               show-checkbox
               default-expand-all
               :default-checked-keys="defkeys"
               :data="rights"
               :props="treeDefaultProps"></el-tree>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="setRightsDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitSetRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      roles: [],
      setRightsDialogVisible: false,
      rights: [],
      treeDefaultProps: {
        label: 'authName'
      },
      defkeys: [],
      roleId: ''
    }
  },

  created () {
    this.getRoles()
  },

  methods: {
    async getRoles () {
      const { data: res } = await this.$axios.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.roles = res.data
    },
    removeTag (row, rightId) {
      this.$confirm('删除是否,继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete(`roles/${row.id}/rights/${rightId}`)
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        row.children = res.data
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    async showSetRights (row) {
      this.setRightsDialogVisible = true
      const { data: res } = await this.$axios.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.rights = res.data
      this.getAllkeys(row)
      this.roleId = row.id
    },
    getAllkeys (row) {
      if (!row.children) return this.defkeys.push(row.id)
      row.children.forEach(item => {
        this.getAllkeys(item)
      })
    },
    closeSetDialog () {
      this.defkeys = []
    },
    async submitSetRights () {
      console.log(this.roleId)
      const checkedkeys = this.$refs.tree.getCheckedKeys()
      const halfCheckedkeys = this.$refs.tree.getHalfCheckedKeys()
      const rids = [...checkedkeys, ...halfCheckedkeys].join(',')
      const { data: res } = await this.$axios.post('roles/' + this.roleId + '/rights', { rids })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.setRightsDialogVisible = false
      this.getRoles()
      this.$message.success('更新成功')
    }
  }
}
</script>
<style scoped lang='less'>
.border_top {
  border-top: 1px solid #ccc;
}
.border_botton {
  border-bottom: 1px solid #ccc;
}
.el-tag {
  margin: 7px;
}
.el-row {
  display: flex;
  align-items: center;
}
</style>
